package com.changyinBus.web.controller.system;

import com.changyinBus.common.annotation.Log;
import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.common.core.domain.entity.*;
import com.changyinBus.common.core.page.TableDataInfo;
import com.changyinBus.common.enums.BusinessType;
import com.changyinBus.common.utils.IDGenerator;
import com.changyinBus.system.service.ISysProdCateService;
import com.changyinBus.system.service.ISysProdService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

/**
 * 产品管理
 */
@RestController
@RequestMapping("/system/prod")
public class SysProdController extends BaseController {
    @Resource
    private ISysProdService sysProdService;
    @Resource
    private ISysProdCateService sysProdCateService;

    /**
     * 获取产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:prod:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProd prod) {
        startPage();
        List<SysProd> list = sysProdService.selectProdList(prod);
        return getDataTable(list);
    }

    /**
     * 根据产品编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prod:query')")
    @GetMapping(value = { "/", "/{prodId}" })
    public AjaxResult getInfo(@PathVariable(value = "prodId", required = false) Long prodId) {
        return success(sysProdService.selectProdById(prodId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('system:prod:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysProd prod) {
        prod.setProdId(IDGenerator.getID());
        prod.setCreateBy(getUsername());
        try {
            //转译带style的content
            prod.setProdContent(new String(Base64.getDecoder().decode(prod.getProdContent()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return success(sysProdService.insertProd(prod));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('system:prod:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysProd prod) {
        prod.setUpdateBy(getUsername());
        try {
            //转译带style的content
            prod.setProdContent(new String(Base64.getDecoder().decode(prod.getProdContent()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return success(sysProdService.updateProd(prod));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:prod:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysProd prod) {
        prod.setUpdateBy(getUsername());
        return success(sysProdService.updateProdStatus(prod));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('system:prod:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prodId}")
    public AjaxResult remove(@PathVariable Long prodId) {
        return success(sysProdService.deleteProdById(prodId));
    }

    /**
     * 获取分类树列表
     */
    @PreAuthorize("@ss.hasPermi('system:prod:list')")
    @GetMapping("/cateTree")
    public AjaxResult cateTree(SysProdCate cate) {
        return success(sysProdCateService.selectCateTreeList(cate));
    }
}
